package com.kx.hackathon.service.impl;

import com.kx.hackathon.service.HandlerService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("DdlServiceImpl")
public class DdlServiceImpl implements HandlerService {
    @Autowired
    OkHttpClient httpClient;
    @Override
    public String handle(String msg, String arg) throws Exception {
        if(arg==null || arg.isEmpty()){
            return "你先出个上联，我给你出下联";
        }
        String url = "http://www.guabu.com/duilian/?key="+arg+"&Submit=在线对联";
        System.out.println(url);
        Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Response response = httpClient.newCall(request).execute();
        String html = response.body().string();
        String dl = html.split("</?h3>")[1].replaceAll("</?[^<>]+>","");
        return dl;
    }

    @Override
    public String init(String msg, String arg) {
        return null;
    }

    @Override
    public boolean needInit() {
        return false;
    }
}
